require "common/Define"
require "framework/BaseController"
require "FairyGUI"
require "ScrollPaneHeader"

PullToRefreshMain = BaseController:New({mControllerName = "PullToRefreshMain"});

function PullToRefreshMain.Main()
	-- body
	PullToRefreshMain:Awake();
end

function PullToRefreshMain:Awake()
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown, self);

	fgui.register_extension("ui://PullToRefresh/Header", ScrollPaneHeader);

	-- attack pacakge, package in resource or assetbundle
	local packageName = "UI/PullToRefresh";
	UIPackage.AddPackage(packageName);

	local componentName = "Main";
	self.mainComponent = UIPackage.CreateObject("PullToRefresh", componentName).asCom;
	GRoot.inst:AddChild( self.mainComponent );

	--local go = GameObject();
	--local panel = go:AddComponent(typeof(UIPanel));
	--panel.container:AddChild(self.mainComponent);
	--GRoot.inst:AddChild( panel );

	-- self.mainComponent:SetSize(GRoot.inst.width, GRoot.inst.height);
	-- self.mainComponent:AddRelation(GRoot.inst, RelationType.Size);
	GRoot.inst:SetContentScaleFactor(1134, 640, FairyGUI.UIContentScaler.ScreenMatchMode.MatchWidthOrHeight);

	self:OnViewCreated(self.mainComponent);
end

function PullToRefreshMain:InitView()
	self._list1 = self.mainComponent:GetChild("list1").asList;
	self._list1.itemRenderer = FairyGUI.ListItemRenderer(self.RenderListItem1, self);
	self._list1:SetVirtual();
	self._list1.numItems = 1;
	self._list1.scrollPane.onPullDownRelease:Add(self.OnPullDownToRefresh, self);

	self._list2 = self.mainComponent:GetChild("list2").asList;
	self._list2.itemRenderer = FairyGUI.ListItemRenderer(self.RenderListItem2, self);
	self._list2:SetVirtual();
	self._list2.numItems = 1;
	self._list2.scrollPane.onPullUpRelease:Add(self.OnPullUpToRefresh, self);
end

function PullToRefreshMain:RenderListItem1( index, obj )
	-- body
	local item = obj.asButton;
	item.title = "Item " .. (self._list1.numItems - index - 1);
end

function PullToRefreshMain:RenderListItem2( index, obj )
	-- body
	local item = obj.asButton;
	item.title = "Item " .. index;
end

function PullToRefreshMain:OnPullDownToRefresh()
	local header = self._list1.scrollPane.header;
	if (header.ReadyToRefresh) then
		header:SetRefreshStatus(2);
		self._list1.scrollPane:LockHeader(header.sourceHeight);

		-- Simulate a async resquest
		FairyGUI.Timers.inst:Add(2, 1, function (param) 
			self._list1.numItems = self._list1.numItems + 5;

			-- Refresh completed
			header:SetRefreshStatus(3);
			self._list1.scrollPane:LockHeader(35);

			FairyGUI.Timers.inst:Add(2, 1, function (param2)
				header:SetRefreshStatus(0);
				self._list1.scrollPane:LockHeader(0);
			end);
		end);
	end
end

function PullToRefreshMain:OnPullUpToRefresh( ... )
	-- body
	local footer = self._list2.scrollPane.footer;

	footer:GetController("c1").selectedIndex = 1;
	self._list2.scrollPane:LockFooter(footer.sourceHeight);

	-- Simulate a async resquest
	FairyGUI.Timers.inst:Add(2, 1, function (param)
			self._list2.numItems = self._list2.numItems + 5;

			-- Refresh completed
			footer:GetController("c1").selectedIndex = 0;
			self._list2.scrollPane:LockFooter(0);
		end);
end

function PullToRefreshMain:OnKeyDown(context)
 	-- body
 	if (context.inputEvent.keyCode == KeyCode.Escape) then
		print "Exit Game!"
		Application.Quit();
	end
end

